# encoding: utf-8
import json
import requests
from fake_useragent import UserAgent
# from fontTools.ttLib import TTFont
# from sql_ac.sql_1 import insert_one, insert_baidu_map
import sys
import io
from lxml import html
import re

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
sys.setrecursionlimit(1000000)
etree = html.etree
ua = UserAgent()
kv = {
    'user-agent': ua.random,
    "Connection": 'close',
    # 'Host': 'm.dianping.com',
    'Origin': 'http://www.dianping.com',
    'X-Request': 'JSON',
    # 'Content-Type': 'application/x-www-form-urlencoded',
    # 'X-Requested-With': 'XMLHttpRequest',
    'Referer': 'http://www.dianping.com/search/map/category',
    # 'cookie': '_lxsdk_cuid=17963780453c8-0f717895c20b43-31614c0c-1fa400-17963780453c8; _lxsdk=17963780453c8-0f717895c20b43-31614c0c-1fa400-17963780453c8; _hc.v=c4e87342-e951-bdb2-c773-dd37f0a21b51.1620871481; s_ViewType=10; _dp.ac.v=e6d174d1-19f7-43d5-8986-cc95ab5475f9; ctu=176b698d6eb22f57f4ac645ddbe1804dfd71c619c9b60e18720d2eac2f2044b8; cityid=7; switchcityflashtoast=1; dp_pwa_v_=fe25b31c1e622e79b2c551c1b73fa76ba19c48e7; cy=7; cye=shenzhen; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1621414201,1621849779,1622001743,1622084206; Hm_lvt_233c7ef5b9b2d3d59090b5fc510a19ce=1621395578,1621827101,1622001834,1622084366; logan_custom_report=; m_flash2=1; pvhistory="6L+U5ZuePjo8L3Nob3AvdGVsOjAyMTYzMzM4Nzc4Pjo8MTYyMjA4NDkwMTA0NF1fWw=="; logan_session_token=kpzmty37nr6nsmbpjyr1; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1622085260; msource=default; Hm_lpvt_233c7ef5b9b2d3d59090b5fc510a19ce=1622087275; _lxsdk_s=179abef8ea8-26e-a37-94b%7C0%7C16; default_ab=citylist%3AA%3A1%7Cshop%3AA%3A11%7Cindex%3AA%3A3%7CshopList%3AC%3A5%7Cmap%3AA%3A1%7Cshopphoto%3AA%3A1'
}


# http://www.dianping.com/shenzhen/ch35/g32745
#  ch  大分类
#   g具体分类
#  r 地点
def main():
    # print(it[2],it[1][25:])
    data = {
        'cityId': 2,  # 城市编码
        'cityEnName': 'beijing',
        'promoId': '0',
        'shopType': '0',  # 大分类
        'categoryId': '0',  # keyid
        'regionId': '0',  # 具体分类
        'sortMode': '2',
        'shopSortItem': '0',
        'keyword': '',
        'searchType': '1',
        'branchGroupId': '0',
        'aroundShopId': '0',
        'shippingTypeFilterValue': '0',
        'page': '1'
    }
    r = requests.post('http://www.dianping.com/search/map/ajax/json', headers=kv, data=data).text
    js = json.loads(r)
    for i in js['shopRecordBeanList']:
        addr = i['address']
        name = i['shopName']
        tag = i['dishTag']
        shopid = i['shopId']
        print(addr, name, tag, shopid)


if __name__ == '__main__':
    # with open('city_id.txt') as f:
    #     citys = f.readlines()
    # for j, i in enumerate(citys):
    #     c = i.replace('\n', '').split('\t')
    #     print(j + 1)
    main()
